package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MusicActivity;
import flc.ast.adapter.CategoryAdapter;
import flc.ast.adapter.FoodAdapter;
import flc.ast.adapter.MenuAdapter;
import flc.ast.databinding.FragmentDietBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DietFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DietFragment extends BaseNoModelFragment<FragmentDietBinding> {

    private MenuAdapter menuAdapter;
    private CategoryAdapter categoryAdapter;
    private FoodAdapter foodAdapter;
    private int oldPosition;


    @Override
    protected int onCreate() {
        return R.layout.fragment_diet;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rvMenuList.setLayoutManager(new GridLayoutManager(mContext, 2));
        menuAdapter = new MenuAdapter();
        mDataBinding.rvMenuList.setAdapter(menuAdapter);
        menuAdapter.setOnItemClickListener(this);

        mDataBinding.rvCategoryList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        categoryAdapter = new CategoryAdapter();
        mDataBinding.rvCategoryList.setAdapter(categoryAdapter);
        categoryAdapter.setOnItemClickListener(this);

        mDataBinding.rvFoodList.setLayoutManager(new GridLayoutManager(mContext, 2));
        foodAdapter = new FoodAdapter();
        mDataBinding.rvFoodList.setAdapter(foodAdapter);
        foodAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == categoryAdapter) {
            categoryAdapter.getItem(oldPosition).setSelected(false);
            oldPosition = position;
            categoryAdapter.getItem(position).setSelected(true);
            categoryAdapter.notifyDataSetChanged();
            getData(categoryAdapter.getItem(position).getHashid());
        } else {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            MusicActivity.stkResBean = bean;
            startActivity(MusicActivity.class);
        }
    }

    @Override
    protected void initData() {
        getMenuData();
        getCategoryData();
    }

    private void getMenuData() {
        StkResApi.getTagResourceList(this, AppConfig.MENU_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        menuAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getCategoryData() {
        StkResApi.getChildTagList(this, AppConfig.FOOD_CATEGORY_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        result.get(0).setSelected(true);
                        categoryAdapter.setList(result);
                        getData(result.get(0).getHashid());
                    }
                }
            }
        });
    }

    private void getData(String hashId) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + hashId, StkResApi.createParamMap(0, 20), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        foodAdapter.setList(result);
                    }
                }
            }
        });
    }

}